﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lead2012.Business;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Repository;

namespace Lead2012.Controllers
{
    public class LeadStatusController : BaseController
    {
        // Create an instance of repository
        LeadStatusManagementRepository amRepository = new LeadStatusManagementRepository();

        [Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            LeadStatusViewModel model = new LeadStatusViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "FirstName");
            amRepository.GetModel(model, string.Empty, string.Empty, string.Empty, 1, 50);
           
            return View(model);
        }

        public ActionResult Filters(string keywords, string sortBy, string sortOrder, int pageIndex, int pageSize)
        {
            LeadStatusViewModel model = new LeadStatusViewModel();

            // Setup model
            if (pageSize == 0)
                pageSize = 10;
            if (pageIndex == 0)
                pageIndex = 1;

            amRepository.GetModel(model, keywords, sortBy, sortOrder, pageIndex, pageSize);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Delete(int id)
        {
            LeadStatusRepository repository = new LeadStatusRepository();
            LeadStatusViewModel model = new LeadStatusViewModel();

            // Delete action
            repository.Delete(id);

            // Setup model
            amRepository.GetModel(model, string.Empty, string.Empty, string.Empty, 1, 10);
            
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Add()
        {
            LeadStatusViewModel model = new LeadStatusViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "FirstName");
            amRepository.GetModel(model, 0);

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(FormCollection form)
        {
            LeadStatusRepository repository = new LeadStatusRepository();
            LeadStatus model = new LeadStatus();
            model.MilestoneId = Convert.ToInt32(form["MilestoneId"].ToString());
            model.Color = form["Color"].ToString();
            model.Title = form["Title"].ToString();
            model.OrderId = amRepository.GetOrderId();
            repository.Create(model);

            return Redirect("/LeadStatus");
        }

        [Authorize(Roles = "Administrator")]
        public ActionResult Edit(int id)
        {
            LeadStatusViewModel model = new LeadStatusViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "FirstName");
            amRepository.GetModel(model, id);
            ViewBag.StatusId = id;

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(int id, FormCollection form)
        {
            LeadStatusRepository repository = new LeadStatusRepository();
            LeadStatus model = new LeadStatus();
            model = repository.GetSingle(id);

            model.MilestoneId = Convert.ToInt32(form["MilestoneId"].ToString());
            model.Color = form["Color"].ToString();
            model.Title = form["Title"].ToString();
            repository.Update(model);
            return Redirect("/LeadStatus");
        }
        
        
        [HttpPost]
        public ActionResult UpdateLeadStatusItems(int id, string order)
        {
            LeadStatusRepository repository = new LeadStatusRepository();
            LeadStatus model = new LeadStatus();

            // Get current field id
            model = repository.GetSingle(id);
            var list = repository.GetAll();
           
            if (list != null)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].StatusId == id)
                    {
                        if (order == "up")
                        {
                            // Set order current item
                            model.OrderId += 1;
                            repository.Update(model);

                            // Set order prev item
                            model = new LeadStatusItem();
                            model = repository.GetSingle(list[i + 1].StatusId);
                            model.OrderId += 1;
                            repository.Update(model);
                        }
                        else
                        {
                            // Set order current item
                            model.OrderId -= 1;
                            repository.Update(model);

                            // Set order prev item
                            model = new LeadStatusItem();
                            model = repository.GetSingle(list[i - 1].StatusId);
                            model.OrderId -= 1;
                            repository.Update(model);
                        }
                        break;
                    }
                }
            }

            list = repository.GetAll();

            return Json(list, JsonRequestBehavior.AllowGet);
        }


    }
}





